﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;

namespace WPFClient.Codes
{
    // 语言信息类
    public class LanguageInfo
    {
        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// 语言代码
        /// </summary>
        public string LanguageCode { get; set; }
    }

    public class LanguageManager : INotifyPropertyChanged
    {
        /// <summary>
        /// 中文
        /// </summary>
        public const string Chinese = "zh-cn";

        /// <summary>
        /// 英文
        /// </summary>
        public const string English = "en-us";

        /// <summary>
        /// 阿拉伯语
        /// </summary>
        public const string Albanian = "sq-al";



        /// <summary>
        /// 当前语言文件名
        /// </summary>
        public string CurrentLanguageFileName { get; set; } = LanguageManager.Chinese;

        private Dictionary<string, string> _currentLanguage = new Dictionary<string, string>();

        /// <summary>
        /// 当前语言
        /// </summary>
        public Dictionary<string, string> CurrentLanguage
        {
            get => _currentLanguage;
            set
            {
                _currentLanguage = value;
                OnPropertyChanged(nameof(CurrentLanguage));
                // 这行很重要：通知所有绑定了索引器的控件更新
                OnPropertyChanged("Item[]");
            }
        }

        /// <summary>
        /// 索引器 - 通过键获取语言值
        /// </summary>
        public string this[string key]
        {
            get
            {
                if (_currentLanguage.TryGetValue(key, out string value))
                    return value;

                return $"[{key}]"; // 找不到时返回键名
            }
        }

        public LanguageManager()
        {
            // 默认使用中文语言
            SetLanguage(LanguageManager.Chinese);
        }

        public void SetLanguage(string languageFileName = LanguageManager.Chinese)
        {
            try
            {
                // 获取配置文件路径
                string assemblyLocation = Assembly.GetExecutingAssembly().Location;
                string appDirectory = Path.GetDirectoryName(assemblyLocation);
                string configPath = Path.Combine(appDirectory, "Languages", $"{languageFileName}.json");

                if (!File.Exists(configPath))
                {
                    throw new FileNotFoundException($"语言配置文件未找到: {configPath}。请确保对应的语言json文件存在于应用Languages目录中。");
                }

                // 读取并解析JSON
                string jsonContent = File.ReadAllText(configPath);
                JObject jsonObject = JObject.Parse(jsonContent);
                Dictionary<string, string> result = new Dictionary<string, string>();

                if (jsonObject != null)
                {
                    foreach (JProperty property in jsonObject.Properties())
                        result.Add(property.Name, property.Value?.ToString());
                }

                this.CurrentLanguageFileName = languageFileName;
                this.CurrentLanguage = result;
            }
            catch (Exception ex) when (!(ex is FileNotFoundException))
            {
                throw new Exception($"解析语言文件时出错: {ex.Message}", ex);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}